package com.${package}.controller;
import com.${package}.model.SkxdMessage;
import com.${package}.service.ISkxdMessageService;
import com.${package}.vo.DataTableVo;
import com.zxs.common.Page;
import com.zxs.resp.ReturnResult;
import com.zxs.util.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;


/**
 * Created by shang-pc on 2015/11/7.
 */
@Controller
@RequestMapping("/admin/${table.lowerClassName}")
public class ${table.className}Controller {

    @Autowired
    private I${table.className}Service ${table.lowerClassName}Service;


    @RequestMapping("/to${table.className}Page")
    public String to${table.className}Page() {
        return "${table.lowerClassName}/${table.tableName}_page";
    }

    @RequestMapping("/${table.lowerClassName}Page")
    @ResponseBody
    public DataTableVo ${table.lowerClassName}Page(DataTableVo paramDataTableVo) throws Exception {
        DataTableVo dataTableVo = null;
        Map params = DataTableVo.cpoyDataTableToMap(paramDataTableVo);
        Page page = ${table.lowerClassName}Service.query${table.className}Page(params);
        dataTableVo = DataTableVo.cpoyPageToDataTable(page);
        dataTableVo.setsEcho(paramDataTableVo.getsEcho());
        return dataTableVo;
    }

    @RequestMapping("/toAdd${table.className}")
    public String toAdd${table.className}(ModelMap model) {
        ${table.className} ${table.lowerClassName}=new ${table.className}();
        model.addAttribute("${table.lowerClassName}",${table.lowerClassName});
        return "${table.lowerClassName}/to_edit_${table.tableName}";
    }

    @RequestMapping("/saveOrUpdate${table.className}")
    @ResponseBody
    public ReturnResult saveOrUpdate${table.className}(${table.className} ${table.lowerClassName}) throws Exception {
        ReturnResult result = null;
        int flag = ${table.lowerClassName}Service.saveOrUpdate${table.className}(${table.lowerClassName});
        if (flag == 0) {
            result = ReturnResultUtil.returnFail();
        } else {
            result = ReturnResultUtil.returnSuccess();
        }
        return result;
    }

    @RequestMapping("/remove${table.className}ByIds")
    @ResponseBody
    public ReturnResult remove${table.className}ByIds(String ids) throws Exception {
        ReturnResult result = ReturnResultUtil.returnSuccess();
        int flag = ${table.lowerClassName}Service.remove${table.className}ByIds(ids);
        if (flag == 0) {
            result = ReturnResultUtil.returnFail();
        } else {
            result = ReturnResultUtil.returnSuccess();
        }
        return result;
    }

    @RequestMapping("/toUpdate${table.className}")
    public String toUpdate${table.className}(String id, ModelMap model) throws Exception {
        ${table.className} ${table.lowerClassName} = ${table.lowerClassName}Service.query${table.className}ById(id);
        model.addAttribute("${table.lowerClassName}", ${table.lowerClassName});
        return "${table.lowerClassName}/to_edit_${table.tableName}";
    }
}
